package com.zhs.study.service.impl;


import com.zhs.study.common.R;
import com.zhs.study.exception.ServiceException;
import com.zhs.study.pojo.User;
import com.zhs.study.dao.UserDao;
import com.zhs.study.service.IUserService;
import com.zhs.study.dto.UserDto;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 *
 * @author zhenghuisheng
 * @since 2023.04.18
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl< UserDao, User> implements IUserService {

    @Resource
    private UserDao userDao;

    @Override
    @Transactional
    public R addOrUpdate(UserDto userDto) {
        if (BeanUtil.isEmpty(userDto)) throw new ServiceException("数据不能为空");
        User user = new User();
        BeanUtil.copyProperties(userDto,user);
        if (userDto.getId() == null){
            userDao.insert(user);
            return R.ok("数据插入成功");
        }else {
            userDao.updateById(user);
            return R.ok("数据更新成功");
        }
    }
}